{*******************************************************}
{*
{* oWorldManager.pas
{* Created on:      24-II-2010
{* Original author: Miroslav Rusin
{*
edit varga - PO_GET_LOGICAL_ENTITIES_FOR_PEDESTRIAN throws error
             PO_GET_PHYSICAL_ENTITY throws error
             PO_GET_LOGICAL_ENTITY throws error
             PO_PREPARE_LOAD_SCENE wrong parameters to function
             PO_SAVE_PROJECT_DATA_TO_DIR undeclared
{*******************************************************}



unit oWorldManager;

interface

uses Classes, oLevel, oCoordinates, oLogicalEntity,oPhysicalEntity,
  oManager, oEntity, oScene, Generics.Collections, oProcessObjectConstants, oProcessItem,
  SysUtils;

type

  EWorldManager = class(Exception)

  end;

  TWorldManager = class (TManager)
  private
    aScene : TScene;
  public
//    function ProcessObject(paMap: TProcessList): TProcessList; override;
    function ProcessObject(paItem: TProcessItem): TProcessItem; override;
    constructor Create; overload;
    constructor Create(paMaster : TManager); overload;
    destructor Destroy; override;
    property Scene : TScene read aScene write aScene;
    procedure test;
  end;

implementation

uses
  oProcessObjects;

{implementation of TWorldManager}

constructor TWorldManager.Create;
begin
  aScene := TScene.Create;
end;

constructor TWorldManager.Create(paMaster: TManager);
begin
  Create;
  aMaster := paMaster;
end;

destructor TWorldManager.Destroy;
begin
  inherited Destroy;
end;

function TWorldManager.ProcessObject(
  paItem: TProcessItem) : TProcessItem;
var
  res : boolean;
  list : Tlist;
  response : TProcessingList;
  pList : TProcessingList;
  xml : TProcessingXML;
  e : Tentity;
  name : Tstring;
  typ : TInteger;
begin
  res := false;
  response := TProcessingList.Create;

  if paItem.Data <> nil then
  if paItem.Data is TProcessingList then
      plist := TProcessingList(paItem.Data)
    else
      {if paItem.Data is TProcessingXML then
        xml := TProcessingXML(paItem.Data)}
      else
        if paItem.Data is TString then
          name := TString(paItem.Data)
        else
         raise EWorldManager.Create('WorldManger: Expected TProcessingList or TString or TProcessingXML instance, got ' +
          paItem.ClassName);

  //list := nil;
  case (paItem.Code) of
    PO_SCENE_PHYSICAL_ENTITY :
       begin
          {res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).insertPhysicalEntity(
          TPhysicalEntity(pList.Parameters[1]));}
       end;
    PO_SCENE_DELETE_PHYSICAL_ENTITY :
       begin
          res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).deletePhysicalEntity(
          TPhysicalEntity(pList.Parameters[1]));
       end;
    PO_SCENE_MOVE_PHYSICAL_ENTITY :
       begin
          res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).movePhysicalEntity(
          TPhysicalEntity(pList.Parameters[1]), TPhysicalEntity(pList.Parameters[2]));
       end;
    PO_SCENE_INSERT_LOGICAL_ENTITY :
       begin
          res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).UpdateLogicalEntity(
          TPhysicalEntity(pList.Parameters[1]), TLogicalEntity(pList.Parameters[2]));
       end;
    PO_SCENE_DELETE_LOGICAL_ENTITY :
       begin
          res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).DeleteLogicalEntity(
          TPhysicalEntity(pList.Parameters[1]));
       end;
    PO_SCENE_UPDATE_LOGICAL_ENTITY :
       begin
          Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).UpdateLogicalEntity(
          TPhysicalEntity(pList.Parameters[1]), TLogicalEntity(pList.Parameters[2]));
          res := True;
       end;
    PO_SCENE_GET_PHYSICAL_ENTITIES_FOR_PEDESTRIAN :
       begin
          {list := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).getPhysicalEntities(
          TPedestrian(pList.Parameters[1]).Position, TPedestrian(pList.Parameters[2]).Scope);
          if list <> nil then res := true;}
       end;
    PO_SCENE_GET_LOGICAL_ENTITIES_FOR_PEDESTRIAN :
       begin
          {list := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).getLogicalEntities(
          TCoordinates(pList.Parameters[1]), TReal(pList.Parameters[2]).Real_);
          if list <> nil then res := true;}
       end;
    PO_SCENE_GET_PEDESTRIANS_FOR_PED_PEDESTRIAN :
       begin
          {list := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).getPedestrians(TPedestrian
          (pList.Parameters[1]));
          if list <> nil then res := true;}
       end;
    PO_SCENE_INSERT_PEDESTRIAN :
       begin
         {res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).insertPedestrian(TPedestrian
          (pList.Parameters[1]));}
       end;
    PO_SCENE_MOVE_PEDESTRIAN :
       begin
         {res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).updatePedestrian(TPedestrian
          (pList.Parameters[1]), TCoordinates(pList.Parameters[2]),
          TCoordinates(pList.Parameters[3]));}
       end;
    PO_SCENE_DELETE_PEDESTRIAN :
       begin
         {res := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).deletePedestrian(
         TPedestrian(pList.Parameters[1]));}
       end;
    PO_SCENE_GET_PHYSICAL_ENTITY :
       begin
          {ist := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).getPhysicalEntities(
          TCoordinates(pList.Parameters[1]), TReal(pList.Parameters[2]).Real_);
          response.addParameter(list[0]);}
       end;
    PO_SCENE_GET_LOGICAL_ENTITY :
       begin
          {list := Scene.getLevel(TInteger(pList.Parameters[0]).Integer_).getLogicalEntities(
          TCoordinates(pList.Parameters[1]), TReal(pList.Parameters[2]).Real_);
          response.addParameter(list[0]);}
       end;
    PO_SCENE_PREPARE_SAVE :
      begin
         //Scene.SaveToXML(name.String_);
      end;
    PO_SCENE_PREPARE_LOAD :
      begin
        try
          {Scene.LoadFromXML(TString(pList.Parameters[0]).String_,
            TStringList(pList.Parameters[1]),
            TStringList(pList.Parameters[2]));}
        except on e : Exception do
          raise EWorldManager.Create('Invalid parameters in TProcessinglist');
        end;
      end;
    PO_SCENE_DELETE_LAYER : begin
        //name???
        //Scene.deleteLevel(name.String_);
    end;
    PO_SCENE_GET_ENTITY_BY_ID : begin
        e := Scene.getLevel(TString(pList.Parameters[0]).String_).getEntity(TInteger(pList.Parameters[1]).Integer_);
        response.addParameter((e));
    end;
    PO_SCENE_GET_LAYER_BY_NAME : begin
        //NAME!!!
        //response.addParameter(Scene.getLevel(name.String_));
    end;
    PO_SCENE_INSERT_LAYER : begin
        name := Tstring(pList.Parameters[0]);
        typ := TInteger(pList.Parameters[1]);
        Scene.addLevel(name.String_, typ.Integer_);
    end;
  end;

  if res then
    Result := TProcessItem.create(PO_SUCCESS, TProcessingObject(response))
  else
    Result := TProcessItem.create(PO_FAILURE, nil);

end;

procedure TWorldManager.test;
var
  level : TLevel;
  s : string;
begin
  level := TLevel.Create(20, 20, 20);
  level.test;
  level.Name := 'phys1';
  level.TypeLayer := oLevel.PHYS_LAYER;
  Scene.addLevel(level);

  level := TLevel.Create(20, 20, 20);
  level.test;
  level.Name := 'log1';
  level.TypeLayer := oLevel.LOG_LAYER;
  Scene.addLevel(level);
  s := 'c:/test/test/test';
  //aMaster.ProcessObject(TProcessItem.create(PO_SAVE_PROJECT_DATA_TO_DIR,TString.Create(s)));
end;

end.
